package top.maochang.hou.servlet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import top.maochang.hou.dao.impl.MenuDaoImpl;
import top.maochang.hou.entity.SysMenu;
import top.maochang.hou.entity.SysUser;
import top.maochang.hou.tools.MenuHelper;
import top.maochang.hou.tools.Result;
import top.maochang.hou.tools.UserContent;
import top.maochang.hou.tools.WebUtlis;
import top.maochang.hou.vo.SysMenuVo;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "menu", urlPatterns = "/menu/*")
public class MenuServlet extends BaseServlet{

    /**
     * 得到菜单数据
     * /menu/getMenuList
     * @param req
     * @param resp
     * @return
     * @throws JsonProcessingException
     */
    void getMenuList(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        //构建菜单数据
        MenuDaoImpl menuDao=new MenuDaoImpl();
        SysUser user= (SysUser) UserContent.getUser();
        //拿到当前用户把id给他
        SysUser  sys_user =(SysUser) UserContent.getUser();
        user.setId(sys_user.getId());
        //查询出来的原始菜单数据
        List<SysMenu> sysMenus = menuDao.queryListMenuByUserId(user);
        //构建上下级菜单数据
        List<SysMenuVo> sysMenuVos = MenuHelper.buildTree(sysMenus);
        //解决一个问题父级菜单的path javascript:;

        //ajax数据处理
        ObjectMapper objectMapper= WebUtlis.getObjectMapper();
        Result<List<SysMenuVo>> stringResult = new Result<List<SysMenuVo>>(200, "菜单数据查询成功", sysMenuVos);
        String s = objectMapper.writeValueAsString(stringResult);
        resp.getWriter().write(s);
    }

}
